package cn.edu.zisu.common.util.model;

import cn.edu.zisu.common.model.request.PagingParam;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author : Sonya
 * @date : 2020/3/23 19:56
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PageRunner {
    public static final Integer PAGE_SIZE = 5000;

    /**
     * 上次查询最后一个id
     */
    private Long lastId;
    /**
     * 最后一页的size
     */
    private int finalSize;
    /**
     * 总次数 n
     * n = length / PAGE_SIZE
     */
    private int times;
    /**
     * 最后一页偏移量
     * offset = length % PAGE_SIZE
     */
    private int offset;

    /**
     * 解析产生PageRunner
     *
     * @param pagedParam 分页参数
     * @return Runner
     */
    public static PageRunner parseRunner(PagingParam pagedParam) {
        int times = pagedParam.getLength() / PAGE_SIZE;
        int finalSize = pagedParam.getLength() % PAGE_SIZE;
        int offset = 0;
        return PageRunner.builder().lastId(0L).finalSize(finalSize).times(times).offset(offset).build();
    }

    public static PageRunner parseRunner(Integer count) {
        int times = count / PAGE_SIZE;
        int finalSize = count % PAGE_SIZE;
        int offset = 0;
        return PageRunner.builder().lastId(0L).finalSize(finalSize).times(times).offset(offset).build();
    }
}
